package cn.junechiu.learnrxjava.net;

import android.util.ArrayMap;

import java.util.concurrent.TimeUnit;

import cn.junechiu.learnrxjava.BuildConfig;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by junzhao on 2017/7/18.
 */

public class RetrofitManager {

    private static ArrayMap<String, CompositeDisposable> netManager = new ArrayMap<>();

    public static Retrofit getInstance() {
        return Instance.retrofit;
    }

    private static class Instance {

        private static String baseUrl = "https://api.github.com/";

        private static Retrofit retrofit = getRetrofit();

        private static Retrofit getRetrofit() {
            OkHttpClient.Builder okHttpBuilder = new OkHttpClient.Builder();
            // debug模式添加log信息拦截
            if (BuildConfig.DEBUG) {
                HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
                interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
                okHttpBuilder.addInterceptor(interceptor);
            }
            // okHttpBuilder.addInterceptor(new HeaderInterceptor());
//            okHttpBuilder.addInterceptor(new ParamsInterceptor(commonParams()));
            // 设置网络连接失败时自动重试
            okHttpBuilder.retryOnConnectionFailure(false);
            okHttpBuilder.connectTimeout(30, TimeUnit.SECONDS);
            okHttpBuilder.writeTimeout(30, TimeUnit.SECONDS);
            okHttpBuilder.readTimeout(30, TimeUnit.SECONDS);

            Retrofit.Builder retrofitBuilder = new Retrofit.Builder();
            retrofitBuilder.baseUrl(baseUrl);
            retrofitBuilder.client(okHttpBuilder.build());
            retrofitBuilder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
            retrofitBuilder.addConverterFactory(GsonConverterFactory.create());
//            retrofitBuilder.addConverterFactory(ScalarsConverterFactory.create()); // 字符串转换器
            return retrofitBuilder.build();
        }

        // 公共参数封装
//        private static ArrayMap<String, String> commonParams() {
//            UserController userController = ControllerFactory.getController(UserController.class);
//            ArrayMap<String, String> params = new ArrayMap<>();
//            params.put("version", HttpConfig.APP_VERSION);
//            if (userController.getUserId() != 0) {
//                params.put("userid", String.valueOf(userController.getUserId()));
//            }
//            if (!TextUtils.isEmpty(userController.getToken())) {
//                params.put("token", String.valueOf(userController.getToken()));
//            }
//            return params;
//        }
    }

    // 为了避免错误的取消了，key建议使用packagename + calssName
    public static void add(String key, Disposable disposable) {
        if (netManager.containsKey(key)) {
            netManager.get(key).add(disposable);
        } else {
            CompositeDisposable compositeDisposable = new CompositeDisposable();
            compositeDisposable.add(disposable);
            netManager.put(key, compositeDisposable);
        }
    }

    public static void remove(String key) {
        if (netManager.containsKey(key)) {
            CompositeDisposable compositeDisposable = netManager.get(key);
            compositeDisposable.clear();
        }
    }
}

